package com.hsck.order.kds.main.fragment.return_deishe

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.hsck.order.kds.R
import com.hsck.order.kds.base.isnoam.log.XLog
import com.hsck.order.kds.base.isnoam.view.BaseFragment
import com.hsck.order.kds.base.isnoam.view.SpacesItemDecoration
import com.hsck.order.kds.base.utils.NetworkUtil
import com.hsck.order.kds.databinding.FragmentReturnOfDishesBinding
import com.hsck.order.kds.main.fragment.standard.bean.DishManager
import com.hsck.order.kds.main.fragment.standard.bean.TestListBean
import com.hsck.order.kds.main.itf.UpdatableFragmentListener

/**
 * 退菜动态
 */
class ReturnOfDishesFragment : BaseFragment<FragmentReturnOfDishesBinding>(),
    UpdatableFragmentListener {
    override fun inflateBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
    ): FragmentReturnOfDishesBinding {
        return FragmentReturnOfDishesBinding.inflate(inflater)
    }

    private var returnOfDishesAdapter: ReturnOfDishesAdapter? = null
    private var itemBean: TestListBean? = null
    private var position: Int? = null

//    override fun loadDataOnce() {
//        super.loadDataOnce()
////        initRecyclerView()
//        showNoDataView {  }
//        returnOfDishesAdapter?.submitList(DishManager().getDishList())
//        binding.llRoot.visibility = View.VISIBLE
//    }

    override fun onResume() {
        super.onResume()
//        initRecyclerView()
        showNoDataView {  }
        returnOfDishesAdapter?.submitList(DishManager().getDishList())
        binding.llRoot.visibility = View.VISIBLE
    }

    private fun initRecyclerView() {
        returnOfDishesAdapter = ReturnOfDishesAdapter(requireActivity())
        val staggeredGridLayoutManager =
            StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL)
        staggeredGridLayoutManager.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_NONE
        staggeredGridLayoutManager.isItemPrefetchEnabled = true
        binding.recyclerView.setHasFixedSize(true)
        binding.recyclerView.setItemViewCacheSize(20)
        binding.recyclerView.layoutManager = staggeredGridLayoutManager
        binding.recyclerView.adapter = returnOfDishesAdapter
        val spacingInPixels = resources.getDimensionPixelSize(R.dimen.four_column_spacing)
        binding.recyclerView.addItemDecoration(SpacesItemDecoration(spacingInPixels))
        returnOfDishesAdapter!!.setDishOperationListener(object :
            ReturnOfDishesAdapter.DishOperationListener {
            override fun onDishOperationAction(itemBean: TestListBean, position: Int) {
                this@ReturnOfDishesFragment.itemBean = itemBean
                this@ReturnOfDishesFragment.position = position
                //判断后台返回是否开启快速划菜
                if (itemBean.openDialog) {

                } else {
                    //请求划菜接口
                }
                XLog.i("数量: ${itemBean.number}, 是否弹窗: ${itemBean.openDialog}")
            }
        })
    }

    override fun onFragmentVisible() {
        if (_binding != null) {
            if (NetworkUtil.isNetworkAvailable(requireActivity())) {
                hideLoadErrorView()
                returnOfDishesAdapter?.submitList(DishManager().getDishList())
                binding.llRoot.visibility = View.VISIBLE
            } else {
                showLoadErrorView {
                    XLog.i("加载失败，请点击重试")
                }
            }
        }
    }

    override fun onFragmentInvisible() {
        returnOfDishesAdapter?.stopAllUpdating()
        binding.llRoot.visibility = View.GONE
        //停止接收MQTT
    }

    override fun onDestroyView() {
        super.onDestroyView()
        returnOfDishesAdapter?.stopAllUpdating()
    }
}